package com.Dao.PocketMoneyDao;

import com.Dao.InterfaceMapper.PoketMoneyMappper;
import com.JavaBean.Pok_Mon.Poketmoney;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.PropertyConfigurator;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.*;

public class Method {
    // 链接
    private static SqlSession sqlSession;

    static {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("mybatis_config.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //从配置文件中获取一个工厂对象

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //通过工厂获取sqlsession对象
        sqlSession = factory.openSession();

    }

    //日志
    public static void rizhi() throws IOException {
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/main/resources/log4j.properties");
        properties.load(fileInputStream);
        PropertyConfigurator.configure(properties);
    }

    //创建实时日期
    public static String createDate() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String format = simpleDateFormat.format(date);
        return format;
    }

    //查找全部
    public static void selectAllbytime() throws IOException {
        rizhi();
        PoketMoneyMappper mapper = sqlSession.getMapper(PoketMoneyMappper.class);
        ArrayList<Poketmoney> poketmonies = mapper.selectAllbytime();
        for (Poketmoney poketmony : poketmonies) {
            System.out.print("\t\t\t\t" + poketmony.getPoject());
            System.out.print("\t\t\t\t " + poketmony.getPay());
            System.out.print("\t\t\t\t " + poketmony.getTime());
            System.out.print("\t\t\t\t " + poketmony.getDeposit());
            System.out.println("");
        }

    }

    //查找总收入(中间变量方便后续加减运算)
    public static int selectdeposit() throws IOException {
        rizhi();
        PoketMoneyMappper mapper = sqlSession.getMapper(PoketMoneyMappper.class);
        ArrayList<Poketmoney> selectdeposit = mapper.selectdeposit();
        int poketmoneyDeposit = selectdeposit.get(0).getDeposit();

        return poketmoneyDeposit;
    }

    //插入数据（加法运算）
    public static void insertdeposit() throws IOException {
        Scanner scanner = new Scanner(System.in);
        rizhi();
        PoketMoneyMappper mapper = sqlSession.getMapper(PoketMoneyMappper.class);

        System.out.println("请输入入账钱数：");
        String next = scanner.next();

        //加法运算
        int selectdeposit = Method.selectdeposit();
        Integer b = selectdeposit + Integer.valueOf(next);
        Date date = new Date();
        //调用实时日期
        String format = Method.createDate();


        Poketmoney poketmoney = new Poketmoney();
        poketmoney.setPoject("收入");
        poketmoney.setPay(Integer.valueOf(next));
        poketmoney.setTime(format);
        poketmoney.setDeposit(b);

        mapper.insert(poketmoney);
        sqlSession.commit();//----------一定要提交事务不然不会入库-------------------
    }

    //插入数据（减法运算）
    public static void insertreducedeposit() throws IOException {
        Scanner scanner = new Scanner(System.in);
        rizhi();
        PoketMoneyMappper mapper = sqlSession.getMapper(PoketMoneyMappper.class);

        System.out.printf("消费金额");
        int i = scanner.nextInt();
        System.out.printf("消费说明");
        String next = scanner.next();
        //减法运算
        int selectdeposit = Method.selectdeposit();
        int b = selectdeposit - i;
        System.out.println("消费后的余额为" + b);
        //调用实时日期
        String format = Method.createDate();


        Poketmoney poketmoney = new Poketmoney();
        poketmoney.setPoject(next);
        poketmoney.setPay(Integer.valueOf(i));
        poketmoney.setTime(format);
        poketmoney.setDeposit(b);

        mapper.insert(poketmoney);
        sqlSession.commit();

    }

}
